package server;

import java.rmi.RemoteException;

public class FirewallManagerImpl implements FirewallManager {

	Commands comm;
	private boolean isReadWriteSessionInProgress;

	public FirewallManagerImpl(Commands comm) {
		this.comm = comm;
	}

	public synchronized FirewallSession login(String username, String password) 
	throws RemoteException, InvalidLoginException, FirewallException {
		boolean isReadWrite = false;
		boolean isExpert = false;

		if (comm != null) {
			Password info;
			info = comm.checkUser(username, password);
			if (info.password.compareTo(password) != 0)
				throw new InvalidLoginException();
			if (info.type == Password.typeOpt.EXPERT)
				isExpert = true;
		}

		if (!isReadWriteSessionInProgress) {
			startReadWriteSession();
            isReadWrite = true;
        }
		
		return new FirewallSessionImpl(this, isExpert, isReadWrite);
	}
	
	private synchronized void startReadWriteSession() {
		isReadWriteSessionInProgress = true;
		System.out.println("FWMgr: starting RW session\n");
	}

	public synchronized void stopReadWriteSession() {
		isReadWriteSessionInProgress = false;
		System.out.println("FWMgr: stopping RW session\n");
	}
}
